package cn.itlaobing.spring.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.itlaobing.spring.dao.StudentDao;
import cn.itlaobing.spring.entity.Student;
import cn.itlaobing.spring.service.StudentsService;

@Service
public class StudentsServiceimpl implements StudentsService {
	
	@Autowired
	private StudentDao studentDao;

	/*List<Student> students = new ArrayList<>();

	public StudentsServiceimpl() {
		for (int i = 0; i < 20; i++) {
			Student student = new Student();
			student.setId(i + 1L);
			student.setName("student" + i);
			student.setBirthday(new Date());
			students.add(student);
		}
	}
*/
	@Override
	public List<Student> findAll() {
		/*Collections.sort(students,new Comparator<Student>(){

			@Override
			public int compare(Student o1, Student o2) {
				Long result=o2.getId().longValue()-o1.getId().longValue();
				return result.intValue();
			}
			
		});*/
		
		List<Student> students =studentDao.findAll();
		return students;
	}

	@Override
	public void save(Student student) {
		/*student.setId(RandomUtils.nextLong());
		student.setName(student.getName());
		students.add(student);*/
		
		studentDao.addStudent(student);
	}

	@Override
	public void delete(Long id) {
		//students.remove(findAllById(id));
		
		studentDao.deleteID(id);
	}

	@Override
	public Student findAllById(Long id) {
		/*for (Student student : students) {
			if(student.getId().longValue()==id.longValue()){
				return student;
			}
		}*/
		
		Student student=studentDao.findById(id);
		
		return student;
	}

	@Override
	public void update(Student student) {
		/*Student stu=findAllById(student.getId());
		if(stu!=null){
	    		stu.setName(student.getName());
			stu.setGender(student.getGender());
			stu.setBirthday(student.getBirthday());
		}*/
		  
		studentDao.update(student);
		
	}

}
